//
//  CreateNewMessageView.m
//  SanYiTeaching
//
//  Created by 李文强 on 2019/4/30.
//  Copyright © 2019 SanYi. All rights reserved.
//

#import "CreateNewMessageView.h"
#import "TeacherClassListModel.h"
@interface CreateNewMessageView ()
{
    UIView *lineView;
    UIView *lineViewCenter;
    //班级模型数组
    NSMutableArray *dataArray;
}
@property (nonatomic, strong) TeacherClassListModel *selectValue;   //选择的值
@property (strong, nonatomic) NSMutableArray<NSString *> *data;
@property (strong, nonatomic) TeacherClassListModel *mainModel;
@property (strong, nonatomic) NSMutableArray *messageClassArray;
@end

@implementation CreateNewMessageView

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}
- (void)initSubView{
    self.view.backgroundColor = [UIColor colorWithWhite:0.5 alpha:0.3];
    _messageClassArray = [NSMutableArray new];
//    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(dismissView:)];
//    [self.view addGestureRecognizer:tap];
    [self.view addSubview:self.baseView];
    [_baseView addSubview:self.titleLabel];
    [_baseView addSubview:self.lineView];
    [_baseView addSubview:self.enterTV];
    [_baseView addSubview:self.selectedClassTitleLabel];

    
    [_baseView addSubview:self.tableList];
    [_baseView addSubview:self.cancleButton];
    [_baseView addSubview:self.confirmButoon];
    lineView = [[UIView alloc]init];
    lineView.backgroundColor = LINCOLOR;
    [_baseView addSubview:lineView];
    
    lineViewCenter = [[UIView alloc]init];
    lineViewCenter.backgroundColor = LINCOLOR;
    [_baseView addSubview:lineViewCenter];
    [self getBanJiListRequest];
}
//- (void)dismissView:(UITapGestureRecognizer *)gesture{
//    NSLog(@"手势 %@",gesture.view);
//    if ([gesture.view isKindOfClass:self.baseView]) {
//
//    }else{
//        [self dismissViewControllerAnimated:NO completion:nil];
//    }
//
//}
- (void)layout{
    [_baseView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(@0);
        make.centerY.equalTo(@0);
        make.width.equalTo(@300);
        make.height.equalTo(@311);
    }];
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view.mas_centerX);
        make.top.equalTo(self->_baseView.mas_top);
        make.height.equalTo(@40);
        make.width.equalTo(self->_baseView.mas_width);
    }];
    [_lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self->_baseView.mas_left);
        make.right.equalTo(self->_baseView.mas_right);
        make.height.equalTo(@1);
        make.top.equalTo(self->_titleLabel.mas_bottom);
    }];
    [_enterTV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self->_baseView.mas_left).offset(16);
        make.right.equalTo(self->_baseView.mas_right).offset(-16);
        make.top.equalTo(self->_lineView.mas_bottom).offset(14);
        make.height.equalTo(@60);
    }];
    [_selectedClassTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self->_baseView.mas_left).offset(16);
        make.right.equalTo(self->_baseView.mas_right).offset(-16);
        make.top.equalTo(self->_enterTV.mas_bottom).offset(25);
        make.height.equalTo(@14);
    }];
    [_tableList mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self->_baseView.mas_left).offset(10);
        make.right.equalTo(self->_baseView.mas_right).offset(-10);
        make.top.equalTo(self->_selectedClassTitleLabel.mas_bottom);
        make.bottom.equalTo(self->_baseView.mas_bottom).offset(-60);
    }];
    [_cancleButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self->_baseView.mas_left).offset(0);
        make.right.equalTo(self->_baseView.mas_centerX).offset(-0.5);
        make.height.equalTo(@40);
        make.bottom.equalTo(self->_baseView.mas_bottom).offset(0);
    }];
    [_confirmButoon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self->_baseView.mas_centerX).offset(0.5);
        make.right.equalTo(self->_baseView.mas_right).offset(0);
        make.height.equalTo(@40);
        make.bottom.equalTo(self->_baseView.mas_bottom).offset(0);
    }];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@0);
        make.bottom.equalTo(self->_baseView.mas_bottom).offset(-41);
        make.right.equalTo(@0);
        make.height.equalTo(@1);
    }];
    [lineViewCenter mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self->_baseView.mas_centerX);
        make.bottom.equalTo(@0);
        make.width.equalTo(@1);
        make.height.equalTo(@40);
    }];
}
- (UIView *)baseView{
    if (!_baseView) {
        UIView *view = [[UIView alloc]init];
        view.backgroundColor = [UIColor whiteColor];
        view.layer.cornerRadius = 5;
        view.layer.masksToBounds = YES;
        _baseView = view;
    }
    return _baseView;
}
- (UILabel *)titleLabel{
    if (!_titleLabel) {
        UILabel *label = [[UILabel alloc]init];
        label.text = @"新建消息";
        label.textAlignment = 1;
        label.textColor = [UIColor darkGrayColor];
        label.font = [UIFont systemFontOfSize:14];
        _titleLabel = label;
    }
    return _titleLabel;
}
- (UIView *)lineView{
    if (!_lineView) {
        UIView *label = [[UIView alloc]init];
        label.backgroundColor = LINCOLOR;
        _lineView = label;
    }
    return _lineView;
}
- (UITextView *)enterTV{
    if (!_enterTV) {
        UITextView *tv = [[UITextView alloc]init];
        tv.layer.cornerRadius = 5;
        tv.layer.borderColor = LINCOLOR.CGColor;
        tv.layer.borderWidth = 1;
        tv.delegate = self;
        _placeHolder = [[UILabel alloc]init];
        _placeHolder.frame = CGRectMake(10, 10, 120, 15);
        _placeHolder.textColor =LINCOLOR;
        _placeHolder.text =@"填写消息内容，100字以内";
        _placeHolder.font = [UIFont systemFontOfSize:16];
        [tv addSubview:_placeHolder];
        _enterTV = tv;
    }
    return _enterTV;
}
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
    if (textView.text.length>0) {
        _placeHolder.hidden = YES;
    }else{
        _placeHolder.hidden = NO;
    }
    return YES;
}
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView{
    if (textView.text.length>0) {
        _placeHolder.hidden = YES;
    }else{
        _placeHolder.hidden = NO;
    }
    return YES;
}
- (UILabel *)selectedClassTitleLabel{
    if (!_selectedClassTitleLabel) {
        UILabel *label = [[UILabel alloc]init];
        label.text = @"选择班级";
        label.textAlignment = 0;
        label.textColor = [UIColor darkGrayColor];
        label.font = [UIFont systemFontOfSize:14];
        _selectedClassTitleLabel = label;
    }
    return _selectedClassTitleLabel;
}

- (UIButton *)cancleButton{
    if (!_cancleButton) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setTitle:@"取消" forState:UIControlStateNormal];
        [button setTitleColor:SELECTEDCOLOR forState:UIControlStateNormal];
        button.titleLabel.font  = [UIFont systemFontOfSize:16];
        [button addTarget:self action:@selector(cancleClick:) forControlEvents:UIControlEventTouchUpInside];
        _cancleButton =button;
    }
    return _cancleButton;
}
- (void)cancleClick:(UIButton *)sender{
    self.cancelBlock();
}
-(UIButton *)confirmButoon{
    if (!_confirmButoon) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setTitle:@"保存" forState:UIControlStateNormal];
        [button setTitleColor:SELECTEDCOLOR forState:UIControlStateNormal];
        button.titleLabel.font  = [UIFont systemFontOfSize:16];
        [button addTarget:self action:@selector(confirmClick:) forControlEvents:UIControlEventTouchUpInside];
        _confirmButoon =button;
    }
    return _confirmButoon;
}
- (void)confirmClick:(UIButton *)sender{
    self.confirmBlock(_enterTV.text, _messageClassArray);
}
- (UITableView *)tableList{
    if (!_tableList) {
        UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 144, self.view.frame.size.width-200, APP_HEIGHT-84) style:UITableViewStylePlain];
        tableView.delegate  =self;
        tableView.dataSource = self;
        tableView.backgroundColor = [UIColor whiteColor];
        [tableView registerClass:[CreateNewMessageCell class] forCellReuseIdentifier:@"CreateNewMessageCell"];
        _tableList = tableView;
    }
    return _tableList;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.data.count;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 30;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CreateNewMessageCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CreateNewMessageCell" forIndexPath:indexPath];
    if (!cell) {
        cell = [[CreateNewMessageCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CreateNewMessageCell"];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.index = indexPath.row;
    TeacherClassListModel *model = dataArray[indexPath.row];
    cell.leftTitleLabel.text = model.name;
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"点击了cell");
    _mainModel = dataArray[indexPath.row];
    if ([_messageClassArray containsObject:_mainModel]) {
        [_messageClassArray removeObject:_mainModel];
    }else{
         [_messageClassArray addObject:_mainModel];
    }
    [[NSNotificationCenter defaultCenter]postNotificationName:@"SelectedTableCellOfCreateNewMessageNotice" object:nil userInfo:@{@"index":[NSString stringWithFormat:@"%ld",(long)indexPath.row]}];
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view = [[UIView alloc]init];
    return view;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *view = [[UIView alloc]init];
    view.frame = CGRectMake(0, 0, self.view.frame.size.width, 0);
    return view;
}
- (void)dealloc{
    [[NSNotificationCenter defaultCenter]removeObserver:self name:@"SelectedTableCellOfCreateNewMessageNotice" object:nil];
}
//选择器数据的加载，从设定的最小日期到当前月
- (NSMutableArray<NSString *> *)data{
    if (!_data) {
        _data = [[NSMutableArray alloc] init];
        dataArray = [NSMutableArray new];
        NSMutableArray *array = [PublickFunctionObject getArrayForDataForKey:ClassListOfTeacherDefalut];
        //循环获取可选月份，从当前月份到最小月份
        for (int i=0; i<array.count; i++) {
            TeacherClassListModel *model = [TeacherClassListModel yy_modelWithJSON:array[i]];
            [dataArray addObject:model];
            [_data addObject:model.name];
        }
        self.selectValue = [dataArray firstObject];
    }
    return _data;
}
#pragma mark - 得到班级列表
- (void)getBanJiListRequest{
    
}
@end
